<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-symbol.html" />
<link rel="prev" href="language.html" />
<link rel="parent" href="language.html" />
<link rel="next" href="node859.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>30.1 parser -- Access Python parse trees</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="30. python Language Services"
  href="language.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="30. python Language Services"
  href="language.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="30.1.1 creating AST Objects"
  href="node859.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="language.html">30. Python Language Services</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="language.html">30. Python Language Services</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node859.html">30.1.1 Creating AST Objects</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION0032100000000000000000">
30.1 <tt class="module">parser</tt> --
         Access Python parse trees</a>
</h1>

<p>
<a name="module-parser"></a>

<p>
<a id='l2h-5407' xml:id='l2h-5407'></a>

<p>
The <tt class="module">parser</tt> module provides an interface to Python's internal
parser and byte-code compiler.  The primary purpose for this interface
is to allow Python code to edit the parse tree of a Python expression
and create executable code from this.  This is better than trying
to parse and modify an arbitrary Python code fragment as a string
because parsing is performed in a manner identical to the code
forming the application.  It is also faster.

<p>
There are a few things to note about this module which are important
to making use of the data structures created.  This is not a tutorial
on editing the parse trees for Python code, but some examples of using
the <tt class="module">parser</tt> module are presented.

<p>
Most importantly, a good understanding of the Python grammar processed
by the internal parser is required.  For full information on the
language syntax, refer to the <em class="citetitle"><a
 href="../ref/ref.html"
 title="Python
Language Reference"
 >Python
Language Reference</a></em>.  The parser itself is created from a grammar
specification defined in the file <span class="file">Grammar/Grammar</span> in the
standard Python distribution.  The parse trees stored in the AST
objects created by this module are the actual output from the internal
parser when created by the <tt class="function">expr()</tt> or <tt class="function">suite()</tt>
functions, described below.  The AST objects created by
<tt class="function">sequence2ast()</tt> faithfully simulate those structures.  Be
aware that the values of the sequences which are considered
``correct'' will vary from one version of Python to another as the
formal grammar for the language is revised.  However, transporting
code from one Python version to another as source text will always
allow correct parse trees to be created in the target version, with
the only restriction being that migrating to an older version of the
interpreter will not support more recent language constructs.  The
parse trees are not typically compatible from one version to another,
whereas source code has always been forward-compatible.

<p>
Each element of the sequences returned by <tt class="function">ast2list()</tt> or
<tt class="function">ast2tuple()</tt> has a simple form.  Sequences representing
non-terminal elements in the grammar always have a length greater than
one.  The first element is an integer which identifies a production in
the grammar.  These integers are given symbolic names in the C header
file <span class="file">Include/graminit.h</span> and the Python module
<tt class="module"><a href="module-symbol.html">symbol</a></tt>.  Each additional element of the sequence represents
a component of the production as recognized in the input string: these
are always sequences which have the same form as the parent.  An
important aspect of this structure which should be noted is that
keywords used to identify the parent node type, such as the keyword
<tt class="keyword">if</tt> in an <tt class="constant">if_stmt</tt>, are included in the node tree without
any special treatment.  For example, the <tt class="keyword">if</tt> keyword is
represented by the tuple <code>(1, 'if')</code>, where <code>1</code> is the
numeric value associated with all <tt class="constant">NAME</tt> tokens, including
variable and function names defined by the user.  In an alternate form
returned when line number information is requested, the same token
might be represented as <code>(1, 'if', 12)</code>, where the <code>12</code>
represents the line number at which the terminal symbol was found.

<p>
Terminal elements are represented in much the same way, but without
any child elements and the addition of the source text which was
identified.  The example of the <tt class="keyword">if</tt> keyword above is
representative.  The various types of terminal symbols are defined in
the C header file <span class="file">Include/token.h</span> and the Python module
<tt class="module"><a href="module-token.html">token</a></tt>.

<p>
The AST objects are not required to support the functionality of this
module, but are provided for three purposes: to allow an application
to amortize the cost of processing complex parse trees, to provide a
parse tree representation which conserves memory space when compared
to the Python list or tuple representation, and to ease the creation
of additional modules in C which manipulate parse trees.  A simple
``wrapper'' class may be created in Python to hide the use of AST
objects.

<p>
The <tt class="module">parser</tt> module defines functions for a few distinct
purposes.  The most important purposes are to create AST objects and
to convert AST objects to other representations such as parse trees
and compiled code objects, but there are also functions which serve to
query the type of parse tree represented by an AST object.

<p>
<div class="seealso">
  <p class="heading">See Also:</p>

  <dl compact="compact" class="seemodule">
    <dt>Module <b><tt class="module"><a href="module-symbol.html">symbol</a></tt>:</b>
    <dd>Useful constants representing internal nodes of
                     the parse tree.
  </dl>
  <dl compact="compact" class="seemodule">
    <dt>Module <b><tt class="module"><a href="module-token.html">token</a></tt>:</b>
    <dd>Useful constants representing leaf nodes of the
                    parse tree and functions for testing node values.
  </dl>
</div>

<p>

<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<a name="CHILD_LINKS"><strong>Subsections</strong></a>

<ul class="ChildLinks">
<li><a href="node859.html">30.1.1 Creating AST Objects</a>
<li><a href="node860.html">30.1.2 Converting AST Objects</a>
<li><a href="node861.html">30.1.3 Queries on AST Objects</a>
<li><a href="node862.html">30.1.4 Exceptions and Error Handling</a>
<li><a href="node863.html">30.1.5 AST Objects</a>
<li><a href="node864.html">30.1.6 Examples</a>
<ul>
<li><a href="node865.html">30.1.6.1 Emulation of <tt class="function">compile()</tt></a>
<li><a href="node866.html">30.1.6.2 Information Discovery</a>
</ul></ul>
<!--End of Table of Child-Links-->
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="30. python Language Services"
  href="language.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="30. python Language Services"
  href="language.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="30.1.1 creating AST Objects"
  href="node859.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="language.html">30. Python Language Services</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="language.html">30. Python Language Services</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node859.html">30.1.1 Creating AST Objects</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
